package cn.z2huo.demo.kafka.spring.producer.properties;

import lombok.Getter;
import lombok.Setter;
import org.apache.kafka.clients.CommonClientConfigs;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.ssl.SslBundles;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@ConfigurationProperties(prefix = "spring.kafka.test-topic")
@Getter
@Setter
public class TestTopicProperties {

    private String topicName;

    private List<String> bootstrapServers;

    private String clientId;

    private KafkaProperties.Security security;

    private KafkaProperties.Ssl ssl;

    private KafkaProperties.Producer producer;

    public Map<String, Object> buildProducerProperties() {
        return this.buildProducerProperties(null);
    }

    public Map<String, Object> buildProducerProperties(SslBundles sslBundles) {
        Map<String, Object> properties = buildCommonProperties(sslBundles);
        properties.putAll(this.producer.buildProperties(sslBundles));
        return properties;
    }

    private Map<String, Object> buildCommonProperties(SslBundles sslBundles) {
        Map<String, Object> properties = new HashMap<>();
        if (this.bootstrapServers != null) {
            properties.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, this.bootstrapServers);
        }
        if (this.clientId != null) {
            properties.put(CommonClientConfigs.CLIENT_ID_CONFIG, this.clientId);
        }
//        properties.putAll(this.ssl.buildProperties(sslBundles));
        properties.putAll(this.security.buildProperties());
        return properties;
    }

}
